﻿using System;
using System.Data;
using System.Text;
using System.Data.OracleClient;
using DBUtility;//请先添加引用

namespace OracleDAL.Common
{
    /// <summary>
    /// 数据访问类CODE。
    /// </summary>
    public class CODE
    {
        public CODE()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string CODEID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from CODE");
            strSql.Append(" where CODEID=:CODEID ");
            OracleParameter[] parameters = {
					new OracleParameter(":CODEID", OracleType.VarChar,50)};
            parameters[0].Value = CODEID;

            return OracleHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Model.Common.CODE model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into CODE(");
            strSql.Append("CODEID,CODEVALUE,SORT,NODEID)");
            strSql.Append(" values (");
            strSql.Append(":CODEID,:CODEVALUE,:SORT,:NODEID)");
            OracleParameter[] parameters = {
					new OracleParameter(":CODEID", OracleType.VarChar,25),
					new OracleParameter(":CODEVALUE", OracleType.VarChar,100),
					new OracleParameter(":SORT", OracleType.VarChar,5),
					new OracleParameter(":NODEID", OracleType.VarChar,64)};
            parameters[0].Value = model.CODEID;
            parameters[1].Value = model.CODEVALUE;
            parameters[2].Value = model.SORT;
            parameters[3].Value = model.NODEID;

            OracleHelper.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Model.Common.CODE model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update CODE set ");
            strSql.Append("CODEVALUE=:CODEVALUE,");
            strSql.Append("SORT=:SORT,");
            strSql.Append("NODEID=:NODEID");
            strSql.Append(" where CODEID=:CODEID ");
            OracleParameter[] parameters = {
					new OracleParameter(":CODEID", OracleType.VarChar,25),
					new OracleParameter(":CODEVALUE", OracleType.VarChar,100),
					new OracleParameter(":SORT", OracleType.VarChar,5),
					new OracleParameter(":NODEID", OracleType.VarChar,64)};
            parameters[0].Value = model.CODEID;
            parameters[1].Value = model.CODEVALUE;
            parameters[2].Value = model.SORT;
            parameters[3].Value = model.NODEID;

            OracleHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string CODEID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CODE ");
            strSql.Append(" where CODEID=:CODEID ");
            OracleParameter[] parameters = {
					new OracleParameter(":CODEID", OracleType.VarChar,50)};
            parameters[0].Value = CODEID;

            OracleHelper.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Common.CODE GetModel(string CODEID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CODEID,CODEVALUE,SORT,NODEID from CODE ");
            strSql.Append(" where CODEID=:CODEID ");
            OracleParameter[] parameters = {
					new OracleParameter(":CODEID", OracleType.VarChar,50)};
            parameters[0].Value = CODEID;

            Model.Common.CODE model = new Model.Common.CODE();
            DataSet ds = OracleHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.CODEID = ds.Tables[0].Rows[0]["CODEID"].ToString();
                model.CODEVALUE = ds.Tables[0].Rows[0]["CODEVALUE"].ToString();
                model.SORT = ds.Tables[0].Rows[0]["SORT"].ToString();
                model.NODEID = ds.Tables[0].Rows[0]["NODEID"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CODEID,CODEVALUE,SORT,NODEID ");
            strSql.Append(" FROM CODE ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return OracleHelper.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            OracleParameter[] parameters = {
                    new OracleParameter(":tblName", OracleType.VarChar, 255),
                    new OracleParameter(":fldName", OracleType.VarChar, 255),
                    new OracleParameter(":PageSize", OracleType.Number),
                    new OracleParameter(":PageIndex", OracleType.Number),
                    new OracleParameter(":IsReCount", OracleType.Clob),
                    new OracleParameter(":OrderType", OracleType.Clob),
                    new OracleParameter(":strWhere", OracleType.VarChar,1000),
                    };
            parameters[0].Value = "CODE";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return OracleHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}

